$(function(){
  //需求1：获取用户信息渲染头像
  getUserInfo()

  $('#logout').on('click',function(){
    //退出询问框
    layer.confirm('确认退出登录吗?', {icon: 3, title:'提示'}, function(index){
    localStorage.removeItem('token')
    location.href = '/big_event-03/login.html'
    layer.close(index)
  })
  })

})

function getUserInfo() {
  axios({
    //请求方式是get   可以省略
    // method:'get',
    url:'/my/userinfo',
    // headers:{
    //   Authorization:localStorage.getItem('token')
    // },

  }).then(res=>{
    // console.log(res.data)
    if (res.data.status !==0){
      return layui.layer.msg(res.data.message)
    }
    renderAvatar(res.data.data)
  })
  

}

function renderAvatar(user){
  let name = user.nickname||user.username
  $('.welcome').html('欢迎 &emsp;'+name)
  if(user.user_pic){
    $('.avatar-text').hide()
    $('.layui-nav-img').show().attr('src',user.user_pic)
  } else {
    $('.layui-nav-img').hide()
    $('.avatar-text').show().html(name[0].toUpperCase())
  }
}

